<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="uname">
        <span style="color: rgba(0, 145, 255, 1)">共计（个）：{{ total }}</span>
      </el-form-item>
      <el-form-item label="" prop="uname">
        <el-select v-model="queryParams.orderSuccessType" placeholder="请选择订单类型">
          <el-option
            v-for="item in ordertypes"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="uname">
        <el-select v-model="queryParams.orderSource" placeholder="请选择预约方式">
          <el-option
            v-for="item in yuyuetypes"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="uname">
        <el-input placeholder="请输入" v-model="querysele" class="input-with-select" style="width: 400px">
          <el-select v-model="order" slot="prepend" placeholder="请选择" style="width: 130px">
            <el-option label="订单号" value="id"></el-option>
            <el-option label="服务内容" value="contentType"></el-option>
            <el-option label="被预约人" value="laborerName"></el-option>
            <el-option label="预约人" value="subUserName"></el-option>
            <el-option label="预约电话" value="subPhone"></el-option>
          </el-select>
          <!-- <el-button slot="append" icon="el-icon-search"></el-button> -->
        </el-input>
        <!-- <el-select v-model="queryParams.serveTypeName" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select> -->
      </el-form-item>
      <!-- <el-form-item label="" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="来源(1在线2电话)" prop="orderSource">
        <el-input
          v-model="queryParams.orderSource"
          placeholder="请输入来源(1在线2电话)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务内容名称" prop="contentTypeName">
        <el-input
          v-model="queryParams.contentTypeName"
          placeholder="请输入服务内容名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务类型名称" prop="serveTypeName">
        <el-input
          v-model="queryParams.serveTypeName"
          placeholder="请输入服务类型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间" prop="subDate">
        <el-date-picker clearable
          v-model="queryParams.subDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预约人名称，地址管理中的姓名" prop="subUserName">
        <el-input
          v-model="queryParams.subUserName"
          placeholder="请输入预约人名称，地址管理中的姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约电话" prop="subPhone">
        <el-input
          v-model="queryParams.subPhone"
          placeholder="请输入预约电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址id" prop="subAddressId">
        <el-input
          v-model="queryParams.subAddressId"
          placeholder="请输入地址id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省市区地址" prop="subAddressStr">
        <el-input
          v-model="queryParams.subAddressStr"
          placeholder="请输入省市区地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家政服务人员id" prop="laborerId">
        <el-input
          v-model="queryParams.laborerId"
          placeholder="请输入家政服务人员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="完成时间" prop="orderSuccessTime">
        <el-date-picker clearable
          v-model="queryParams.orderSuccessTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人id" prop="createId">
        <el-input
          v-model="queryParams.createId"
          placeholder="请输入创建人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="updateId">
        <el-input
          v-model="queryParams.updateId"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="订单号" align="center" prop="id" min-width="100" show-overflow-tooltip />
      <el-table-column label="订单时间" align="center" prop="createTime" min-width="100" />
      <!-- <el-table-column label="来源(1在线2电话)" align="center" prop="orderSource" /> -->
      <el-table-column label="预约方式" align="center" prop="orderSource" />
      <el-table-column label="预约时间" align="center" prop="subDate" show-overflow-tooltip />
      <el-table-column label="服务内容" align="center" prop="contentTypeName" show-overflow-tooltip />
      <el-table-column label="被预约人" align="center" prop="laborerName" show-overflow-tooltip />
      <el-table-column label="预约人" align="center" prop="subUserName" width="180" />
      <!-- <el-table-column label="预约人名称，地址管理中的姓名" align="center" prop="subUserName" /> -->
      <el-table-column label="预约电话" align="center" prop="subPhone" />
      <el-table-column label="预约地址" align="center" prop="subAddressStr" show-overflow-tooltip />
      <el-table-column label="完成类型" align="center" prop="orderSuccessType">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSuccessType == 1" style="color: green">已完成</span>
          <span v-if="scope.row.orderSuccessType == 2" style="color: red">已撤销</span>
          <span v-if="scope.row.orderSuccessType == 3" style="color: #ff8800">已预约</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="完成类型" align="center" prop="subAddressStr" /> -->
      <!-- <el-table-column label="订单状态(1:预约2:完成)" align="center" prop="orderStatus" />
      <el-table-column label="完成类型(1:自动完成2:撤销完成)" align="center" prop="orderSuccessType" />
      <el-table-column label="家政服务人员id" align="center" prop="laborerId" />
      <el-table-column label="完成时间" align="center" prop="orderSuccessTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderSuccessTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="撤销方式(1:在线2:客服)" align="center" prop="orderRepealType" />
      <el-table-column label="创建人id" align="center" prop="createId" />
      <el-table-column label="修改人" align="center" prop="updateId" /> -->
      <!-- icon="el-icon-edit" -->
      <!-- icon="el-icon-delete" -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.orderSuccessType == 3"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >预约详情</el-button>
          <el-button
            v-if="scope.row.orderSuccessType == 1"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >已完成详情</el-button>
          <el-button
            v-if="scope.row.orderSuccessType == 2"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >已撤销详情</el-button>
          <el-button
            v-if="scope.row.orderSuccessType == 3"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:remove']"
          >撤销</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 预约详情对话框 -->
    <el-dialog :title="title" :visible.sync="detdia" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="订单信息">
          <span>订单号：{{ form.id }}</span>
          <span style="margin-left: 20px">订单时间：{{ form.createTime }}</span>
          <span v-if="form.orderSource == 1" style="margin-left: 20px">预约方式：在线</span>
          <span v-if="form.orderSource == 2" style="margin-left: 20px">预约方式：电话</span>
          <span v-if="title == '已撤销订单详情'" style="margin-left: 20px">撤销时间：{{ form.orderSuccessTime }}</span><br>
          <span v-if="form.orderRepealType == 1 && title == '已撤销订单详情'" style="margin-left: 20px">撤销方式：在线撤销</span>
          <span v-if="form.orderRepealType == 2 && title == '已撤销订单详情'" style="margin-left: 20px">撤销方式：客服撤销</span>
        </el-form-item>
        <el-form-item label="预约人信息">
          <span v-if="form.yhMinUser != null">{{ form.yhMinUser.uname }}：{{ form.yhMinUser.uphone }}</span>
          <span v-if="form.yhMinUser != null" style="margin-left: 20px">{{ form.yhMinUser.address }}</span>
          <el-button
            style="margin-left: 20px"
            size="mini"
            type="text"
            @click="userdet(form.yhMinUser.id, form.yhMinUser.uphone)"
            v-hasPermi="['system:order:edit']"
          >用户详情</el-button>
        </el-form-item>
        <el-form-item label="服务地址">
          <span>{{form.subAddressStr }}</span>
        </el-form-item>
        <el-form-item label="被预约信息">
          <img style="width: 100px" :src="form.jzLaborer.iconUrl" alt="">
          <span>{{ form.jzLaborer.serveTypeName }}：{{ form.subDate }}</span>
          <span style="margin-left: 20px">{{ form.jzLaborer.name }} {{ form.jzLaborer.addressOneStr }} {{ form.jzLaborer.addressThreeStr }}</span>
          <span style="margin-left: 20px">{{ form.jzLaborer.age }} |  {{ form.jzLaborer.yearNum }}年工作经验  |  {{ form.contentTypeName }}</span>
          <span style="margin-left: 20px">{{ form.jzLaborer.serveMoney }}</span>
          <el-button
            style="margin-left: 20px"
            size="mini"
            type="text"
            @click="shedet(form.jzLaborer.id)"
            v-hasPermi="['system:order:edit']"
          >详情</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitdet">确 定</el-button> -->
        <el-button @click="canceldet">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/system/order";
import { chexiaoFuWu } from "@/api/system/laborer.js";

export default {
  name: "Order",
  data() {
    return {
      imgyrl: 'http://192.168.31.148:8090',
      ordertypes: [
        {
          value: '1',
          label: '已完成'
        }, {
          value: '2',
          label: '已撤销'
        }, {
          value: '3',
          label: '已预约'
        }, {
          value: '',
          label: '全部订单'
        }
      ],
      yuyuetypes: [
        {
          value: '1',
          label: '在线预约'
        }, {
          value: '2',
          label: '电话预约'
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家政订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderSuccessType: null,
        orderSource: null,
      },
      querysele: '',
      order: '',
      // 表单参数
      form: {
        jzLaborer: {},
        yhMinUser: {}
      },
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "预约人id不能为空", trigger: "blur" }
        ],
        orderSource: [
          { required: true, message: "来源(1在线2电话)不能为空", trigger: "blur" }
        ],
        contentType: [
          { required: true, message: "服务内容不能为空", trigger: "change" }
        ],
        contentTypeName: [
          { required: true, message: "服务内容名称不能为空", trigger: "blur" }
        ],
        serveType: [
          { required: true, message: "服务类型(1:住家2白班3钟点工)不能为空", trigger: "change" }
        ],
        serveTypeName: [
          { required: true, message: "服务类型名称不能为空", trigger: "blur" }
        ],
        subDate: [
          { required: true, message: "预约时间不能为空", trigger: "blur" }
        ],
        subAddressStr: [
          { required: true, message: "省市区地址不能为空", trigger: "blur" }
        ],
        orderStatus: [
          { required: true, message: "订单状态(1:预约2:完成)不能为空", trigger: "blur" }
        ],
        laborerId: [
          { required: true, message: "家政服务人员id不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "删除标志不能为空", trigger: "blur" }
        ]
      },
      detdia: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询家政订单列表 */
    getList() {
      this.loading = true;
      const ordername = this.order
      let parse = {
        ...this.queryParams,
      }
      parse[ordername] = this.querysele
      // console.log(parse)
      listOrder(parse).then(response => {
        response.rows.forEach(item => {
          if(item.orderSource == 1){
            item.orderSource = '在线'
          } else {
            item.orderSource = '电话'
          }
        })
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    canceldet() {
      this.detdia = false;
      // this.reset();
    },
    /** 预约详情按钮操作 */
    handleUpdate(row) {
      // console.log(row.id)
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        console.log(response)
        this.form = response.data;
        this.detdia = true;
        if(row.orderSuccessType == 3){
          this.title = "预约订单详情";
        } else if(row.orderSuccessType == 2){
          this.title = "已撤销订单详情";
        } else if(row.orderSuccessType == 1){
          this.title = "已完成订单详情";
        }
      });
    },
    // 用户详情跳转
    userdet(id, uphone){
      this.detdia = false;
      this.$router.push({
        path: '/userdet',
        query: {
          id: id,
          uphone: uphone
        }
      })
    },
    // 家政详情跳转
    shedet(id){
      this.detdia = false;
      // console.log(id)
      this.$router.push({
        path: '/housedet',
        query: {
          id: id
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        orderSource: null,
        contentType: null,
        contentTypeName: null,
        serveType: null,
        serveTypeName: null,
        subDate: null,
        subUserName: null,
        subPhone: null,
        subAddressId: null,
        subAddressStr: null,
        orderStatus: 0,
        orderSuccessType: null,
        laborerId: null,
        orderSuccessTime: null,
        orderRepealType: null,
        createId: null,
        createTime: null,
        updateId: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        orderSuccessType: null,
        orderSource: null,
      }
      this.querysele = ''
      this.order = ''
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加家政订单";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('确认撤销当前预约的服务吗？', '撤销提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          chexiaoFuWu({ id: ids, orderSuccessType: 2, orderStatus: 2  }).then(res => {
            this.$message({
              type: 'success',
              message: '撤销成功!'
            });
            this.getList();
          });
        }).catch(() => {

        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
